package com.gdcp.System.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdcp.System.mapper.UserMapper;
import com.gdcp.handle.MyException;
import com.gdcp.response.RestCode;
import com.gdcp.response.Result;
import com.gdcp.System.entity.User;
import com.gdcp.System.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wrl
 * @since 2021-05-22
 */
@RestController
//@CrossOrigin
@RequestMapping("/user")
@Api(value = "用户管理",tags = "用户管理")
public class UserController {
//    返回用户列表
   @Autowired
   private UserService userService;

         @ApiOperation(value = "用户列表",notes = "分页查询用户列表")
         @GetMapping("/findUserList")
        public Result getUserList(@RequestParam(required = true,defaultValue = "1")Integer current,@RequestParam(required = true,defaultValue = "6")Integer size ){
             if(current>1){
                 current=(current-1)*size+1;
             }
             List<User> userList = userService.splitpage(current, size);
             List<User> list = userService.list();
             int total=list.size();
             return Result.ok().data("total",total).data("user",userList);
         }

         @ApiOperation(value = "用户查询",notes = "通过Id查询单个用户")
         @GetMapping("/{id}")
         public Result findUserById(@PathVariable("id") Long id){
             User user = userService.getById(id);
             if(user!=null){
                 return Result.ok().data("user",user);
             }else {
                throw  new MyException(RestCode.USER_ACCOUNT_NOT_EXIST.getCode(),RestCode.USER_ACCOUNT_NOT_EXIST.getMessage());
             }
         }


}

